package com.jzy.bxb.manage.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.jzy.bxb.core.handler.HttpCode;
import com.jzy.bxb.core.util.HttpUtil;
import com.jzy.bxb.manage.core.vo.ResultVO;
import com.jzy.bxb.manage.service.UserService;
import com.jzy.bxb.model.constant.AppConstant;
import com.jzy.bxb.model.mongo.entity.User;

/**
 * 用户
 *
 * @author JiangZhiYong
 * @date 2017-05-23 QQ:359135103
 */
@Controller
@RequestMapping("/user")
public class UserController {
	private static final Logger LOGGER = LoggerFactory.getLogger(UserController.class);
	@Autowired
	private UserService userService;
	
	@Value(value="${game.url}")
	private String gameUrl;

	/**
	 * 登录
	 * 
	 * @param session
	 * @param param
	 * @return
	 */
	@RequestMapping(value = "/login")
	public ModelAndView login(HttpSession session, HttpServletResponse response, User param) {
		User result = null;
		try {
			result = userService.login(param);
		} catch (Exception e) {
			LOGGER.error("登录", e);
			return new ModelAndView("redirect:/login");
		}
		if (result != null) {
			session.setAttribute(AppConstant.USER, result);
			userService.saveCookie(session, result, response);
			return new ModelAndView("redirect:/home");
		} else {
			return new ModelAndView("redirect:/login");
		}
	}
	
	

	/**
	 * 登录
	 * <p>
	 * 所有用户
	 * </p>
	 * TODO 条件查询,分页处理
	 * 
	 * @param session
	 * @param param
	 * @return
	 */
	@RequestMapping(value = "/list")
	public ModelAndView userList(HttpSession session, HttpServletResponse response) {
		try {
			List<User> users = userService.getUsers();
			return new ModelAndView("/user", "users", users);
		} catch (Exception e) {
			LOGGER.error("登录", e);
			return new ModelAndView("redirect:/login");
		}
	}
	
	/**
	 * 获取用户
	 * @param session
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/get")
	public ResultVO<User> getUser(HttpSession session, HttpServletResponse response, @RequestParam long userId){
		User user = userService.get(userId);
		if(user==null){
			return new ResultVO<User>(HttpCode.ERROR, String.format("用户%d不存在", userId), null);
		}
		return new ResultVO<User>(HttpCode.SUCCESS, "", user);
	}

	/**
	 * 设置、取消帮手
	 * 
	 * @param session
	 * @param param
	 * @param type 0 添加地址 1删除地址
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/set.assistant")
	public ResultVO<String> setAssistant(HttpSession session, HttpServletResponse response, @RequestParam int type,
			@RequestParam long userId, String city, String county,@RequestParam String community) {
		Map<String, String> paramsMap=new HashMap<>();
		paramsMap.put("type", String.valueOf(type));
		paramsMap.put("userId", String.valueOf(userId));
		paramsMap.put("city", city);
		paramsMap.put("county", county);
		paramsMap.put("community", community);
		paramsMap.put("password", AppConstant.MANAGE_AUTH);
		String httpPost = HttpUtil.getInstance().httpPost(gameUrl+"/user/assistant/address", paramsMap);
		return new ResultVO<String>(HttpCode.SUCCESS, httpPost, null);
	}

}
